package com.sample.action;


import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.RoleForm;
import com.sample.javabean.Menu;
import com.sample.javabean.Role;
import com.sample.util.Pager;

/**
 * 角色管理模块
 * 
 * @author 演示
 * 
 */
public class RoleAction extends BaseAction {

	private final static Log log = LogFactory.getLog(RoleAction.class);
	private RoleForm roleForm = new RoleForm();
	private Integer id;
	private Pager<Role> pageList;
	private List<Menu> menuList;
	private String keywords;

	// 使用new()操作符实例化领域模型
	private Role role = new Role();

	/**
	 * 添加角色
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add()throws Exception{
		try{
			log.info("add Role:"+roleForm.getRolename());
			if(roleForm!=null){
				role.setRoleid(roleForm.getRoleid());
				role.setRolename(roleForm.getRolename());
				role.setRemark(roleForm.getRemark());
				role.setMenulistid(roleForm.getMenulistid());
				role.addRole();	
			}
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	/**
	 * 显示角色记录列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listRole() throws Exception {
		try {

			pageList = role.list(page, size, roleForm,keywords);
			return "listRole";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	public String inputRole() throws Exception {
		try {
			Menu menu = new Menu();
			// 加载菜单信息信息列表
			menuList = menu.findMenuList();

			return "inputRole";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public String editRole() throws Exception {
		try {
			role.setId(id);
			role.load();
			Menu menu = new Menu();
			// 加载菜单信息信息列表
			menuList = menu.findMenuList();
			return "editRole";
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		try {
			if (roleForm != null) {
				role.setRoleid(roleForm.getRoleid());
				role.setRolename(roleForm.getRolename());
				role.setRemark(roleForm.getRemark());
				role.setId(roleForm.getId());
				role.setMenulistid(roleForm.getMenulistid());
				role.editRole();
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		try {
			if (roleForm != null) {
				role.setId(id);
				role.delRole();
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}



	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public RoleForm getRoleForm() {
		return roleForm;
	}

	public void setRoleForm(RoleForm roleForm) {
		this.roleForm = roleForm;
	}

	public Pager<Role> getPageList() {
		return pageList;
	}

	public void setPageList(Pager<Role> pageList) {
		this.pageList = pageList;
	}

	public Role getRole() {
		return role;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public void setRole(Role role) {
		this.role = role;
	}
	
}
